/*
 * @Copyright: 2017 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.model.to;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.yyfax.commons.lang.DateUtil;
import com.yyfax.commons.lang.IdCardValidateUtil;
import com.yyfax.commons.lang.ValidateUtil;
import com.yyfax.framework.yyfax.enums.RspCodeEnum;
import com.yyfax.framework.yyfax.enums.TerminalEnum;
import com.yyfax.pay.common.constants.PayConstants;
import com.yyfax.pay.common.exception.YYPayException;
import com.yyfax.pay.common.model.to.YYPayParams;
import com.yyfax.pay.common.util.CommonIdUtils;

/**
 * 支付下单参数
 * 
 * @author jiangmy
 * @date 2017-04-11 13:56:44
 * @since v1.0.0
 */
@SuppressWarnings("serial")
public class PayOrderParams extends YYPayParams {
	private String userIp; // 用户端请求IP
	private String serverIp; // 服务器请求Ip
	private String merchantCode; // 商户编号
	private String userId; // 商户用户ID
	private String registerTime; // 用户注册时间
	private String orderId; // 商户订单号
	private String orderTime; // 订单时间
	private String expireTime; // 过期时间
	private String terminal; // 终端
	private String goodsName; // 物品名称
	private String goodsDesc; // 物品描述
	private String goodsUrl; // 物品路径
	private String paymentCode; // 偏好支付通道编号
	private String returnUrl; // 回显url
	private String notifyUrl; // 异步通知url
	private int amount; // 金额
	private String name; // 姓名
	private String idNo; // 身份证号
	private String cardNo; // 银行卡号

	public String getUserIp() {
		return userIp;
	}

	public void setUserIp(String userIp) {
		this.userIp = userIp;
	}

	public String getServerIp() {
		return serverIp;
	}

	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}

	public String getMerchantCode() {
		return merchantCode;
	}

	public void setMerchantCode(String merchantCode) {
		this.merchantCode = merchantCode;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getRegisterTime() {
		return registerTime;
	}

	public void setRegisterTime(String registerTime) {
		this.registerTime = registerTime;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getOrderTime() {
		return orderTime;
	}

	public void setOrderTime(String orderTime) {
		this.orderTime = orderTime;
	}

	public String getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(String expireTime) {
		this.expireTime = expireTime;
	}

	public String getTerminal() {
		return terminal;
	}

	public void setTerminal(String terminal) {
		this.terminal = terminal;
	}

	public String getGoodsName() {
		return goodsName;
	}

	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName;
	}

	public String getGoodsDesc() {
		return goodsDesc;
	}

	public void setGoodsDesc(String goodsDesc) {
		this.goodsDesc = goodsDesc;
	}

	public String getGoodsUrl() {
		return goodsUrl;
	}

	public void setGoodsUrl(String goodsUrl) {
		this.goodsUrl = goodsUrl;
	}

	public String getPaymentCode() {
		return paymentCode;
	}

	public void setPaymentCode(String paymentCode) {
		this.paymentCode = paymentCode;
	}

	public String getReturnUrl() {
		return returnUrl;
	}

	public void setReturnUrl(String returnUrl) {
		this.returnUrl = returnUrl;
	}

	public String getNotifyUrl() {
		return notifyUrl;
	}

	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIdNo() {
		return idNo;
	}

	public void setIdNo(String idNo) {
		this.idNo = idNo;
	}

	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	@Override
	public boolean validate() throws Exception {
		if (StringUtils.isBlank(this.userIp)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "userIp不能为空", "用户IP不能为空");
		}

		if (StringUtils.isBlank(this.serverIp)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "serverIp不能为空", "服务器Ip不能为空");
		}

		if (StringUtils.isBlank(this.merchantCode)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "merchantCode不能为空", "商户号不能为空");
		}

		if (StringUtils.isBlank(this.userId)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "userId不能为空", "用户id不能为空");
		}

		if (StringUtils.isBlank(this.orderId) || this.orderId.length() > CommonIdUtils.PAY_ORDER_MAX_LENGTH) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "orderId不能为空/长度不能超过" + CommonIdUtils.PAY_ORDER_MAX_LENGTH + "位",
					"订单号不能为空/长度不能超过" + CommonIdUtils.PAY_ORDER_MAX_LENGTH + "位");
		}

		if (StringUtils.isBlank(this.orderTime)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "orderTime不能为空", "订单时间不能为空");
		} else {
			// 校验时间格式
			if (!DateUtil.vaildFormat(orderTime, DateUtil.FORMAT_DATETIME)) {
				throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "orderTime 时间格式不正确" + DateUtil.FORMAT_DATETIME,
						"订单时间时间格式不正确" + DateUtil.FORMAT_DATETIME);
			}
		}

		if (StringUtils.isNotBlank(this.expireTime)) {
			// 校验时间格式
			if (!DateUtil.vaildFormat(expireTime, DateUtil.FORMAT_DATETIME)) {
				throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "expireTime 时间格式不正确" + DateUtil.FORMAT_DATETIME,
						"失效时间格式不正确" + DateUtil.FORMAT_DATETIME);
			}

			// 校验过期时间是否失效
			if (DateUtil.str2DateTime(expireTime).compareTo(new Date()) <= 0) {
				throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "expireTime 无效时间", "失效时间无效");
			}
		}

		if (StringUtils.isBlank(this.terminal) && !TerminalEnum.PC.isValue(this.terminal) && !TerminalEnum.H5.isValue(this.terminal)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "terminal不能为空，或者不支持的终端类型", "terminal不能为空，或者不支持的终端类型");
		}

		if (StringUtils.isBlank(this.returnUrl)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "returnUrl不能为空", "returnUrl不能为空");
		}

		if (StringUtils.isBlank(this.notifyUrl)) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "notifyUrl不能为空", "notifyUrl不能为空");
		}

		if (this.amount <= 0) {
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "amount 不能小于等于 0 分", "金额不能小于等于 0 分");
		}

		// 如果银行卡号不为空， 姓名和证件号码也不能为空
		if (StringUtils.isNotBlank(this.cardNo)) {
			this.cardNo = this.cardNo.trim();
			if (StringUtils.isBlank(this.idNo) || StringUtils.isBlank(this.name)) {
				throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "传参cardNo, 则 idNo/name不能为空", "身份证号或者姓名不能为空");
			}
		}

		// 如果姓名和证件号码存在非空值
		if (StringUtils.isNotBlank(this.idNo) || StringUtils.isNotBlank(this.name)) {
			if (StringUtils.isBlank(this.idNo) || StringUtils.isBlank(this.name)) {
				throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "idNo/name不能为空", "身份证号或者姓名不能为空");
			} else {

				this.idNo = this.idNo.trim();
				// 身份证号
				if (!IdCardValidateUtil.isIdCard(this.idNo)) {
					throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "idNo非法", "身份证号非法");
				}

				this.name = this.name.trim();

				if (this.name.length() > PayConstants.NAME_MAX_LENGTH) {
					throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "name非法", "姓名不能超过最大长度" + PayConstants.NAME_MAX_LENGTH);
				}

				// 校验姓名
				if (!ValidateUtil.isName(this.name)) {
					throw new YYPayException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID, "name非法", "姓名非法，只支持中文姓名");
				}
			}
		}

		return true;
	}

	@Override
	public String[] signFields() {
		return new String[] { "userIp", "serverIp", "merchantCode", "userId", "registerTime", "orderId", "orderTime", "expireTime", "terminal",
				"goodsName", "goodsDesc", "goodsUrl", "paymentCode", "returnUrl", "notifyUrl", "amount", "name", "idNo", "cardNo" };
	}
}
